angular.module("lamallApp")
// 插入的到页脚
.directive('myDirective',function(){
		return {
			templateUrl: 'views/footer.html',
			restrict: 'ECMA',
			replace: false
		}
	})
.controller("homeController",function($scope,homeService,$timeout,$interval){
    homeService.getItems(function(res){
		$scope.homeData=res;
		$scope.homepage=res.data.list;
		$timeout(function(){
			homeBanner();
			homeRoll();
			homeSeckill(res.data.list.length);
			homeSelect($scope);
		},100)
    })
    //还有机会 即将开售数据
    homeService.getpage(home2,home3);
   	function home2(res){
   		$scope.homepage2=res.data.list;
   	}
   	function home3(res){
   		$scope.homepage3=res.data.list;
   		for(var i=0;i<$scope.homepage3.length;i++){
   			$scope.homepage3[i].saleStatus=true;
   			$scope.homepage3[i].saleText="√ 开卖提醒";
    	}
   	}
    // 点击变色 
    $scope.homeChange=function(index){	
    	$scope.bg=[];
    	$scope.bg[index]='home_select_change';
		index==2 ? $scope.soldstatus="true" : $scope.soldstatus="false";
		index==2 ? $scope.selectImgStatus="home_select_img" : $scope.selectImgStatus="";
    }
    $scope.onSale=function(select){
    	for(var i=0;i<$scope.homepage3.length;i++){
    		console.log($scope.homepage3[i])
    		if($scope.homepage3[i]==select){
    			$scope.homepage3[i].saleStatus=!$scope.homepage3[i].saleStatus;
    			$scope.homepage3[i].saleStatus==true ?$scope.homepage3[i].saleText="× 取消提醒" : $scope.homepage3[i].saleText="√ 开卖提醒";		
    		}
    	}
    }
    //秒杀倒计时
    $interval(function(){
    	var  nowtime=new Date();
    	var m=60-nowtime.getMinutes();
    	m<10 ? m="0"+m :m=m;
    	var s=60-nowtime.getSeconds();
    	s<10 ? s="0"+s :s=s;
    	var h=21-nowtime.getHours();
    	var hs=21-nowtime.getHours();
    	h<10 ? h="0"+h :h=h; 
    	hs<0 ? $scope.date="已结束" : $scope.date=h+":"+m+":"+s;  	
    },1000)


});
//轮播图
function homeBanner(){
		var mySwiper = new Swiper('.swiper-container', {
		autoplay: 2000,//可选选项，自动滑动
			//分页器
		pagination : '.swiper-pagination',
		paginationClickable :true,
	})
}	 
//字母滚动
function homeRoll(){
	var roll=document.querySelector(".home_roll_wrap ul");
	var liH=roll.querySelector("li").offsetHeight;
	var index=0; 
	setInterval(function (){ 
		index>=2 ? index=0:index++            
        roll.style.transition = 'all .5s';   
        roll.style.transform = 'translateY('+index*liH*-1+'px)';
    },1000)
}
//抢购
function homeSeckill(index){
	var ul=document.querySelector(".home_seckill_commodity ul");
	var liw=ul.querySelector("li").offsetWidth;
	ul.style.width=liw*(index+10)+"px";
}
//滚动后滚定定位
function homeSelect($scope){
	var ul=document.querySelector("#home_select");
	var ulH=ul.offsetTop-ul.offsetHeight;
	document.onscroll=function(ev){	
		var scrollTop=document.body.scrollTop;
		scrollTop>ulH ? ul.className="homefixed" : ul.className="home_select";
		scrollTop>ulH ? $scope.homeAnchor="home_top" : $scope.homeAnchor="";
	}
}	
